package com.xxx.baseapplication.vm

import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.xxx.baseapplication.util.AbsentLiveData
import com.xxx.baseapplication.vo.BannerVO


class ArticleVM : BaseViewModel() {

    open fun refresh(refreshLayout : RefreshLayout) {
        page.value = 0
        refreshing.value = true
    }

    private val bannerList = Transformations.switchMap(page) {
        if (it == 0) {
            api.bannerList()
        } else {
            AbsentLiveData.create()
        }
    }

    val banners: LiveData<List<BannerVO>> = Transformations.map(bannerList) {
        loading.value = false
        it.data ?: ArrayList()
    }


    private val articleList = Transformations.switchMap(page) {
        api.articleList(it)
    }

    val _articlePage = mapPage(articleList)
    val articlePage = Transformations.map(_articlePage) {
       /* it?.datas?.forEach { a ->
            a.read = dao.isRead(username, a.id)
        }*/
        it
    }

}